package com.ly.lw.styledo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.BrandAllAdapter;
import com.ly.lw.styledo.bean.Brand;
import com.ly.lw.styledo.bean.BrandLogo;
import com.ly.lw.styledo.bean.ResultBrand;
import com.ly.lw.styledo.config.UrlConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sks on 15-7-7.
 */
public class BrandAllFragment extends Fragment {

    @ViewInject(R.id.brand_list)
    private ListView listView;
    private BrandAllAdapter brandAllAdapter;
    private ArrayList<BrandLogo> listBrandLogo = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_brand_all_layout, container, false);
        ViewUtils.inject(this, view);
        initView();
        loadData();
        return view;
    }

    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, UrlConstants.BRAND_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获取服务器返回的json数据
                String result = responseInfo.result;
                Brand brand = JSONObject.parseObject(result, Brand.class);
                if (brand != null) {
                    ResultBrand resultBrand = brand.getResult();

                    if (resultBrand != null) {
                        List<BrandLogo> brandLogo = resultBrand.getList();
                        listBrandLogo.addAll(brandLogo);
                        brandAllAdapter.notifyDataSetChanged();
                    }


                }


            }

            @Override
            public void onFailure(HttpException e, String s) {


            }
        });


    }

    private void initView() {
        brandAllAdapter = new BrandAllAdapter(listBrandLogo, getActivity());
        listView.setAdapter(brandAllAdapter);

    }
}
